package com.jwds.digital.bios.business.service.service.basic.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.digital.bios.business.core.infrastructure.dto.basic.BasicOperateLogReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.basic.assembler.BasicOperateLogAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltAnomalyInfoAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.basic.BasicOperateLog;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltAnomalyInfo;
import com.jwds.digital.bios.business.core.infrastructure.mapper.basic.BasicOperateLogMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.basic.BasicOperateLogRespVO;
import com.jwds.digital.bios.business.service.service.basic.IBasicOperateLogService;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description BasicOperateLogServiceImpl
 * @Author xiao  xun
 * @Date 2024-12-07
 */
@Service
public class BasicOperateLogServiceImpl extends ServiceImpl<BasicOperateLogMapper,BasicOperateLog> implements IBasicOperateLogService {
@Resource
private BasicOperateLogMapper basicOperateLogMapper;
    @Override
    public PageResponse<BasicOperateLogRespVO> page(PageRequest<BasicOperateLogReqDTO> request) {
        BasicOperateLogReqDTO condition = request.getQueryCondition();
        Page<BasicOperateLogRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<BasicOperateLogRespVO> pageData = basicOperateLogMapper.pageQuery(page, condition);
        return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
    }

    @Override
    public List<BasicOperateLogRespVO> list(BasicOperateLogReqDTO reqDto) {
        return basicOperateLogMapper.queryList(reqDto);
    }

    @Override
    public void add(BasicOperateLogReqDTO reqDto) {

        BasicOperateLog entity = BasicOperateLogAssembler.dto2entity(reqDto);
        basicOperateLogMapper.insert(entity);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchAdd(List<BasicOperateLogReqDTO> reqDtoList) {
        reqDtoList.forEach(this::add);
    }

    @Override
    public void update(BasicOperateLogReqDTO reqDto) {
        BasicOperateLog oldEntity = basicOperateLogMapper.selectById(reqDto.getId());
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        BasicOperateLog newEntity = BasicOperateLogAssembler.dto2entity(reqDto, oldEntity);
        basicOperateLogMapper.updateById(newEntity);
    }

    @Override
    public void delete(Long id) {
        BasicOperateLog oldEntity = basicOperateLogMapper.selectById(id);
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        basicOperateLogMapper.deleteById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public BasicOperateLogRespVO queryById(Long id) {
        if(id == null) {
            return null;
        }
        BasicOperateLog entity = baseMapper.selectById(id);
        return BasicOperateLogAssembler.entity2dto(entity);
    }
}
